<template>
    <el-form :model="form" label-width="auto" style="max-width: 600px">
       <el-form-item label="主诉">
           <el-input v-model="form.readme" />
       </el-form-item>
       <el-form-item label="现病史">
           <el-input v-model="form.present" />
       </el-form-item>
       <el-form-item label="现病治疗情况">
           <el-input v-model="form.presentTreat" />
       </el-form-item>
       <el-form-item label="既往史">
           <el-input v-model="form.history" />
       </el-form-item>
       <el-form-item label="过敏史">
           <el-input v-model="form.allergy" />
       </el-form-item>
       <el-form-item label="体格检查">
           <el-input v-model="form.physique" />
       </el-form-item>
       <el-form-item label="检查检验建议">
           <el-input v-model="form.proposal" />
       </el-form-item>

       <el-form-item label="注意事项">
           <el-input v-model="form.careful" />
       </el-form-item>
       <el-form-item label="断诊结果">
           <el-input v-model="form.diagnosis" />
       </el-form-item>
       <el-form-item label="理处意见">
           <el-input v-model="form.cure" />
       </el-form-item>
       <el-form-item>
           <el-button @click="saveRecord">保存</el-button><el-button>重置</el-button>
       </el-form-item>
    </el-form>

</template>

<script setup>
import httpRequest from '@/common';
import { ElMessage } from 'element-plus';
import {ref,getCurrentInstance, onMounted,defineProps, watch} from 'vue';

const {proxy} = getCurrentInstance();

// 接收父组件传值  挂号的id值
const props = defineProps({
   rid:{
       type: Number,
       required: true
   }
});

// 表单双向数据绑定的值
const form = ref({
   readme: '',
   present: '',
   presentTreat: '',
   history: '',
   allergy: '',
   physique: '',
   proposal: '',
   careful: '',
   diagnosis: '',
   cure: ''
})

//  进入该页面说明病例已经创建出来了   先查询该病例
//进入组件就进行查询
const getRecordById = ()=>{

   // 在onMounted中直接获取props中父组件的传值会获取不到  设置一个定时任务
   setTimeout(()=>{
       console.log('--------rid--------',props.rid);

       httpRequest(
           proxy,
           'get',
           '/medicalrecord/register/'+props.rid,
           null,
           resultBean=>{
               form.value = resultBean.data;
           }
       )

   },200);

}

//  保存病例信息    因为上面查询过了  所以有主键  所以最终执行的是更新
// 点击保存按钮触发
const saveRecord = ()=>{

   httpRequest(
       proxy,
       'post',
       '/medicalrecord',
       form.value,
       resultBean=>{
           ElMessage.success('保存成功');
       }
   )

}

onMounted(()=>{
   getRecordById();  //先查询
})

</script>

<style>
</style>